package com.zhong.gang.app.ui.zhuchang.input;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import android.content.Intent;
import android.view.View;
import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.databinding.ActivityZhuChangBinding;
import com.zhong.gang.app.db.UserBean;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.app.ui.contract.ContractAct;
import com.zhong.gang.app.ui.contract.ContractBean;
import com.zhong.gang.app.utils.ShouFaView;
import com.zhong.gang.applib.base.act.ActBase;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.controller.ImgController;
import com.zhong.gang.applib.ui.adapter.AddImgAdapter;
import com.zhong.gang.applib.ui.matching.MatchingAct;
import com.zhong.gang.applib.ui.matching.MatchingBean;
import com.zhong.gang.applib.ui.view.ViewClickedListener;

public class ZhuChangAct extends ActBase<ActivityZhuChangBinding> implements ViewClickedListener  , ShouFaView.MonenyChangedListener {
    private ZhuChangPresenter presenter;
    private ImgController imgController;
    private AddImgAdapter zjAdapter,bdAdapter;
    @Override
    public int LayoutRes() {
        return R.layout.activity_zhu_chang;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "新增驻厂接单";
    }

    @Override
    public void initView() {
        bind.setAct(this);
        bind.shoufaLayout.setData(UserUtils.getIntent(this).getUser().getSteelMillId(),1,0.0);
        presenter = new ZhuChangPresenter(this,bind);
        bind.shoufaLayout.setText(bind.delete);
        bind.delete.setVisibility(View.GONE);
        bind.shipperView.setListener(this);
        bind.zhangkouLayout.setCheckStatus(false);

        imgController = new ImgController();
        zjAdapter = imgController.initImgAdapter(this,bind.zjRecyclerView, Config.ZJ_NUMBER);
        bdAdapter = imgController.initImgAdapter(this,bind.bdRecyclerView,Config.BD_NUMBER);
        bind.zjRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        bind.bdRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        imgController.setNeedImg(zjAdapter,this,true,Config.ZJ_NUMBER,true);
        imgController.setNeedImg(bdAdapter,this,true,Config.BD_NUMBER,true);
        imgController.addImgView(zjAdapter,"",true);
        imgController.addImgView(bdAdapter,"",true);
        bind.shoufaLayout.setListener(this);
    }


    /**
     * 点击事件
     * @param view
     */
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.contract_layout://合同
                startActivityForResult(new Intent(this, ContractAct.class),Config.CONTRACT_NUMBER);
                break;
            case R.id.sure://确定
                presenter.statrt(imgController.getFile(zjAdapter),imgController.getFile(bdAdapter));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)return;
        if (data == null)return;
        imgController.onActivityResult(requestCode == Config.ZJ_NUMBER?zjAdapter:bdAdapter,resultCode,data);
        if (requestCode == Config.USER)
            bind.shipperView.setData((MatchingBean) data.getSerializableExtra(Config.USER_IFNO));
        else if (requestCode == Config.CONTRACT_NUMBER){
            ContractBean.ContractMaterialTypesBean bean = (ContractBean.ContractMaterialTypesBean) data.getSerializableExtra(Config.CONTRACT);
            presenter.setContractMaterial(bean);
        }
    }

    @Override
    public void onViewClicked() {
        UserBean userBean = UserUtils.getIntent(this).getUser();
        MatchingAct.openAct(this, Api.getConsignorinfo
                ,userBean.isLogin(),userBean.getToken(),userBean.getId()+""
                ,UserUtils.getIntent(this).getRoleId()+"",true,false, Config.USER,false);

    }

    @Override
    public void afterMonenyChanged(double allMoneny) {
        bind.zhangkouLayout.setMoneny(allMoneny);
    }
}
